using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.Themes.ControlDefault;
using Telerik.WinControls.UI;
using Logic.Sequential;
using Data.DataTranslator;
using System.Collections;

namespace WindowsApplication2.Gui
{
    public partial class Seq : UserControl
    {
       #region member parameters
        private RadSortOrder[] sortOrder;

        private int numberOfClusters;
        private ArrayList[] clusters;
        private ArrayList saveData;
        private Cell[] results;
        private int[] widthOfCols;
       #endregion
        #region saveDataClass
        private class saveCell
        {
            private string nameOfGenome;
            private int clusterNumber;
            private int genomeCode;
            public saveCell(string nameOfGenome, int clusterNumber, int genomeCode)
            {
                this.nameOfGenome = nameOfGenome;
                this.clusterNumber = clusterNumber;
                this.genomeCode = genomeCode;
            }

            public override string ToString()
            {
                return this.genomeCode.ToString() + "\t" + this.clusterNumber.ToString() + "\t" + this.nameOfGenome;
            }
        }
        #endregion
        #region constructors
        public Seq()
        {
            InitializeComponent();
            sortOrder = new RadSortOrder[3];
            sortOrder[0] = RadSortOrder.None;
            sortOrder[1] = RadSortOrder.None;
            sortOrder[2] = RadSortOrder.None;
            radGridView1.Columns[0].SortOrder = sortOrder[0];
            radGridView1.Columns[1].SortOrder = sortOrder[1];
            radGridView1.Columns[2].SortOrder = sortOrder[2];
            radGridView1.MasterGridViewTemplate.EnableSorting = true;
        }
        public void InitializeDataToShow(Cell[] results, int numberOfClusters)
        {
            //initialize the number of arraylists
            this.numberOfClusters = numberOfClusters;
            this.clusters = new ArrayList[this.numberOfClusters];
            this.results = results;
            for (int i = 0; i < this.numberOfClusters; i++)
                this.clusters[i] = new ArrayList();

            //Gets the partition of the genomes
            foreach (Cell c in results)
                this.clusters[c.GroupNumber - 1].Add(c.GenomeNumber);
            //Only a way of knowing the number of properties
            string[] properties = kDimMap.getPropertiesOfGenome((int)this.clusters[0][0]);
            this.widthOfCols = new int[properties.Length + 1];
            properties = null;
            ImageList imageListSmall = new ImageList();
            for (int i = 0; i < this.numberOfClusters; i++)
            {
                Bitmap bm = new Bitmap(16, 16);
                for (int loop1 = 15; loop1 >= 0; loop1--)
                    for (int loop2 = 15; loop2 >= 0; loop2--)
                        bm.SetPixel(loop1, loop2, kDimMap.HLStoRGB((int)((i / (double)this.numberOfClusters) * 240), 120, 240));
                imageListSmall.Images.Add(bm);
            }
            this.radGridView1.SmallImageList = imageListSmall;
            ShowClusterNames();
            ShowAllGenomeDetails();
            setColsWidth();
        }
        private void setColsWidth()
        {
            int allColsWidth = this.radGridView1.Width - 20;
            int allMaximalLengths = 0;
            for (int i = 0; i < this.widthOfCols.Length; i++)
                allMaximalLengths = allMaximalLengths + this.widthOfCols[i];
            for (int i = 0; i < this.widthOfCols.Length; i++)
                this.radGridView1.Columns[i].Width = (int)((this.widthOfCols[i] / (double)allMaximalLengths) * allColsWidth);
        }

        private void ShowAllGenomeDetails()
        {
            for (int i = 0; i < this.numberOfClusters; i++)
                this.ShowGenomeDetailsOfCluster(i);

        }

        private void ShowClusterNames()
        {
            this.rlistB_ClusterNames.Items.Clear();
            RadListBoxItem element = new RadListBoxItem();
            element.Text = "All genomes";
            this.rlistB_ClusterNames.Items.Add(element);
            for (int i = 1; i <= this.numberOfClusters; i++)
            {
                element = new RadListBoxItem();
                element.Text = "Cluster No." + i.ToString();
                this.rlistB_ClusterNames.Items.Add(element);
            }
        }

        private void ShowGenomeDetailsOfCluster(int clusterId)
        {
            for (int i = 0; i < this.clusters[clusterId].Count; i++)
            {
                string genomeName = kDimMap.getGenomeName((int)this.clusters[clusterId][i]);
                if (this.widthOfCols[0] < genomeName.Length)
                    this.widthOfCols[0] = genomeName.Length;
                string[] properties = kDimMap.getPropertiesOfGenome((int)this.clusters[clusterId][i]);
                string[] oneRaw = new string[properties.Length + 2];
                oneRaw[0] = genomeName;
                for (int j = 1; j < oneRaw.Length-1; j++)
                {
                    oneRaw[j] = properties[j - 1];
                    if (this.widthOfCols[j] < properties[j - 1].Length)
                        this.widthOfCols[j] = properties[j - 1].Length;
                }
                oneRaw[oneRaw.Length - 1] = clusterId.ToString();
                radGridView1.Rows.Add(oneRaw);
            }
        }

        #endregion

        private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            if (e.CellElement.ColumnInfo.HeaderText == "Genome name")
            {
                e.CellElement.TextImageRelation = TextImageRelation.ImageBeforeText;
                e.CellElement.ImageAlignment = ContentAlignment.MiddleLeft;
                e.CellElement.Image = radGridView1.SmallImageList.Images[Int32.Parse(e.CellElement.RowInfo.Cells[3].Value.ToString())];
            }
            else
            {
                e.CellElement.BackColor = SystemColors.ControlLight;
            }
        }

        private void rlistB_ClusterNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = this.rlistB_ClusterNames.SelectedIndex;
            this.radGridView1.Rows.Clear();
            if (selectedIndex == 0)
            {
                ShowAllGenomeDetails();
            }
            else
                ShowGenomeDetailsOfCluster(this.rlistB_ClusterNames.SelectedIndex - 1);
        }
    }
}
